// pages/video/video.
import request from "../../utils/request"
var i = 0
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoGroupList: [],
        navid: '',
        videoList: [],
        urls: [],
        vids: [],
        reurls: [],
        isrefresher: false,
        height: '',
        width: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            height: wx.getSystemInfoSync().windowHeight,
            width: wx.getSystemInfoSync().windowWidth
          })
          console.log(this.data.height)
        this.getvideoGroupListData();
    },
    async getvideoGroupListData(){
        let videoGroupListData = await request("/video/group/list")
        this.setData({
            videoGroupList: videoGroupListData.data.slice(0, 14),
            navid: videoGroupListData.data[0].id
        })
        this.getVideoList(this.data.navid);
    },
    async getVideoList(navid){
        let videoListData = await request("/video/group", {id: navid})
        console.log(videoListData)
        let index = 0;
        let videoList = videoListData.datas.map(item => {
            item.id = index++;
            return item;
        })
        
        this.setData({
            videoList,
            isrefresher: false
        })
        while(this.data.videoList.length > i){
            this.setData({
                vids: this.data.vids.concat(videoList[i].data.vid)
            })
            this.getUrls(this.data.vids[i]) 
           i ++;
        }
        i = 0;
    },
     async getUrls(vid){
        let urlsData = await request("/video/url", {id: vid})
        this.setData({
            urls: this.data.urls.concat(urlsData),
            reurls: this.data.reurls.concat(urlsData.urls[0].url)
        })
    },
    changeNav(event){
        let navid = event.currentTarget.id//event传参会把int转成string ===判断类型和内容 ==只判断内容
        this.setData({
            navid: navid,
            vids: [],
            urls: [],
            reurls: []
        })
        this.getVideoList(this.data.navid)
    },
    refresh(){
        console.log("刷新")
        this.setData({
            isrefresher: true,
            vids: [],
            urls: [],
            reurls: []
        })
        this.getVideoList(this.data.navid)
    },
    play(event){
        console.log("play")
        let id = event.currentTarget.id;
        this.videoContent
        this.videoContent && this.videoContent.stop();
        this.videoContent = wx.createVideoContext(id)
    },
    tolower(){
        console.log('触底')
        this.setData({
            x: ''
        })
    },
    searchMusic(){
        wx.navigateTo({
          url: '/pages/search/search',
        })
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})